package com.sky.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * 工作台业务
 */
@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 查询今日运营数据
     * @return
     */
    public BusinessDataVO businessData() {
        Date date = new Date();
        Date beginOfDay = DateUtil.beginOfDay(date);
        LocalDateTime beginTime = DateUtil.toLocalDateTime(beginOfDay);
        log.info("开始时间--->{}",beginTime);
        Date endOfDay = DateUtil.endOfDay(date);
        LocalDateTime endTime = DateUtil.toLocalDateTime(endOfDay);
        log.info("结束时间--->{}",endTime);
        //营业额
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("beginTime",beginTime);
        map.put("endTime",endTime);
        Double turnover = orderMapper.SumAmountByMap(map); //select sum(amount) from orders where order_time >= beginTime and order_time <= endTime
        turnover = turnover == null? 0.0 : turnover;
        log.info("营业额：{}",turnover);

        //有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.CountOrdersByMap(map);
        validOrderCount = validOrderCount == null? 0 : validOrderCount;
        log.info("有效订单数为：{}",validOrderCount);
        //订单完成率
        map.remove("status");
          //总订单数
        Integer totalOrders = orderMapper.CountOrdersByMap(map);
        totalOrders = totalOrders == null ? 0 : totalOrders;
        double orderCompletionRate = 0.0;
        if (totalOrders != 0){
            orderCompletionRate = 1.0 * validOrderCount / totalOrders;
        }
        log.info("订单完成率为：{}",orderCompletionRate);
        //平均客单价
        Double unitPrice = 0.0;
        if (validOrderCount != 0){
            unitPrice = turnover / validOrderCount;
        }
        log.info("平均客单价为：{}",unitPrice);
        //新增用户数
        Integer newUsers = userMapper.countUserByMap(map);
        log.info("新增用户数为：{}",newUsers);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers == null? 0 : newUsers)
                .build();
    }


    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO overviewSetmeals() {
        // 已启售数量
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countSetmealsEnableOrDisable(map);
        // 已停售数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countSetmealsEnableOrDisable(map);

        return SetmealOverViewVO.builder()
                .sold(sold == null ? 0 : sold)
                .discontinued(discontinued == null ? 0 : discontinued)
                .build();
    }


    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO overviewDishes() {
        //已启售数量
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countDishesEnableOrDisable(map);

        // 已停售数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countDishesEnableOrDisable(map);

        return DishOverViewVO.builder()
                .sold(sold == null ? 0 : sold)
                .discontinued(discontinued == null ? 0 : discontinued)
                .build();
    }


    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO overviewOrders() {
        //待接单数量
        Map<String, Object> map = MapUtil.newHashMap();
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.CountOrdersByMap(map);

        //待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.CountOrdersByMap(map);

        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.CountOrdersByMap(map);

        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.CountOrdersByMap(map);

        //全部订单
        map.remove("status");
        Integer allOrders = orderMapper.CountOrdersByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }
}
